/**
 * 公众号预览
 * @authors Albert (you@example.org)
 * @date    2016-08-15 10:53:43
 * @version $Id$
 */
require( '$d/css/common.less' );
require( './style.less' );
define( [
  'jquery',
  'require',
  'header',
  'message',
  'api',
  'pagination',
  'artCategoryHandle',
  'combiner',
  'bootstrap',
  'bigwe_utils',
  'spread_filter',
  'constants'
], function( $, require ) {
  var ShowTitleMessage = require( 'message' ),
    showTime = ShowTitleMessage.showTime,
    BIGWE_API = require( 'api' ),
    category = require( 'artCategoryHandle' ).category,
    category_color = require( 'artCategoryHandle' ).category_color,
    getBWCode = require( 'artCategoryHandle' ).getBWCode,
    getUrlParam = require( 'bigwe_utils' ).getUrlParam,
    c = require( 'constants' );
  var public_vars = public_vars || {};
  var win_search = window.location.search;
  var params_biz = {
    url: BIGWE_API.SEARCH.SEARCH_PROFILE,
    type: 'post',
    data: {
      biz: $( '.profile_data' ).data( 'biz' ),
      platform: $( '.profile_data' ).data( 'platform' ),
      page: 1,
    },
    callBack: biz_list
  }
  public_vars.$wrap = $( '.sunmap-content' );
  public_vars.page = true;

  /*搜索*/
  public_vars.$wrap.find( 'span.search_btn' ).click( function() {
    public_vars.page = true;
    params_biz.data[ 'page' ] = 1;
    params_biz.data[ 'query' ] = $( '#search_text' ).val();
    myAjax( params_biz );
  } );

  /*select筛选*/
  $( '.selectpicker' ).selectpicker( 'refresh' );
  public_vars.$wrap.find( '.filter-select select.selectpicker' ).change( function() {
    params_biz.data[ 'cr' ] = $( this ).val();
    params_biz.data[ 'page' ] = 1;
    public_vars.page = true;
    myAjax( params_biz );
  } );

  function myAjax( opt ) {
    ShowTitleMessage.show( '正在加载...' );
    $.ajax( {
      type: opt[ 'type' ],
      url: opt[ 'url' ],
      data: opt[ 'data' ],
      success: function( data ) {
        opt[ 'callBack' ]( data );
      },
      error: function( XMLHttpRequest, textStatus, errorThrown ) {
        console.log( textStatus );
        console.log( XMLHttpRequest.status );
        console.log( XMLHttpRequest.readyState );
      },
      complete: function() {
        ShowTitleMessage.hide();
      }
    } );
  }

  function biz_list( data ) {
    var json = eval( '(' + data + ')' );
    if ( json.base_info.platform != 'bigwe.com' ) {
      $( ".total_number" ).text( json.total_rows );
      if ( json.ret != 0 ) {
        $( '.main-list' ).empty().append( '<p class="no_content"><span class="glyphicon glyphicon-flash"></span>暂无内容</p>' );
        ShowTitleMessage.hide();
        $( ".total_number" ).text( 0 );
        $( '.paging' ).hide();
        return;
      }
    } else {
      $( ".total_number" ).text( json.articles.total_num );
    }
    var arts_html = '';
    var cds = '';
    var original = '';
    var list_details = json.base_info.platform == 'bigwe.com' ? json.articles : json.data;
    for ( var i in list_details ) {
      if ( i != 'total_num' ) {
        var art = list_details[ i ];
        var cs = art.copyright_stat * 1; //原创标识
        var original = ( cs == 1 || cs == 2 ) ? ( '<img src="' + c.resPath + '/images/original' ) + ( art.source == 'bigwe.com' ? '' : '2' ) + '.png" alt="" class="original"/>' : '';
        var img_src = art.cover_img_url != '' ? ( c.surpportHostName + '/timthumb.php?w=130&src=' + art.cover_img_url ) : ( c.resPath + '/images/wuyulantu.png' );
        //判断平台icon
        var source_icon = null;
        if ( art.source == 'bigwe.com' ) {
          source_icon = 'icon-iconbigwe';
        } else if ( art.source == 'toutiao.com' ) {
          source_icon = 'icon-icontoutiao icon_font20';
        } else if ( art.source == 'mp.weixin.qq.com' ) {
          source_icon = 'icon-weixin';
        }
        arts_html +=
          '<div class="article-sub ' + ( art[ 'cover_img_url' ] ? '' : 'no_cover' ) + '">' +
          '    <div class="article-title">' +
          '        <span class="hide" style="background:' + category_color[ art.category_code ] + '">' + category[ art.category_code ] + '</span>' +
          '        <a href="/article?p=' + art.cd + '" target="_blank">' + art.title + '</a>' +
          '    </div>' +
          '    <p id="' + art.cd + '" class="js_read_like invisible">阅读:<span>-</span>点赞:<span>-</span></p>';


        if ( art.cover_img_url != '' || art.source == 'bigwe.com' || art.source == 'mp.weixin.qq.com' ) {
          arts_html += '<div class="picture">' +
            '        <a href="/article?p=' + art.cd + '" target="_blank"><img src="' + img_src + '" alt=""></a>' +
            '' + original +
            '    </div>';
        }

        arts_html += '    <div class="article-sub-taxt" data-articleid="' + art.cd + '" data-title="' + art.title + '" data-img="' + img_src + '" data-url="/article?p=' + art.cd + '" data-id="' + art.cd + '">' +
          '        <p>' + art.summary + '</p>' +
          '        <span class="iconfont ' + source_icon + ' platform-icon"></span>' +
          '        <span class="article-account">' + art.publisher + '</span>' +
          '        <span class="article-time">' + ( new Date( art.publish_time_millis * 1 ) ).format( 'yyyy-MM-dd hh:mm' ) + '</span>' +
          '        <span class="iconfont article-collect icon-collect" data-toggle="tooltip" title="收藏"></span>' +
          '        <span class="iconfont article-merge icon-box" addcar-event data-toggle="tooltip" title="加入合成器"></span>' +
          '    </div>' +
          '</div>';

        cds += ',' + art.cd;
      }
    }
    if ( json.base_info.platform == 'bigwe.com' ) {
      if ( json.articles.total_num == '0' ) {
        $( '.main-list' ).empty().append( '<p class="no_content"><span class="glyphicon glyphicon-flash"></span>暂无内容</p>' );
      } else {
        $( '.main-list' ).empty().append( arts_html );
      }
    } else {
      if ( json.total_rows == '0' ) {
        $( '.main-list' ).empty().append( '<p class="no_content"><span class="glyphicon glyphicon-flash"></span>暂无内容</p>' );
      } else {
        $( '.main-list' ).empty().append( arts_html );
      }
    }

    if ( public_vars.page == true ) {
      paging( json.base_info.platform == 'bigwe.com' ? json.articles.total_num : json.total_rows );
    }

    get_msg_ext( cds.substring( 1 ) );
  }

  /*文章点赞数和阅读数*/
  function get_msg_ext( cds ) {
    $.get( '/assets/get_msg_ext?cd=' + cds, function( obj ) {
      var items = obj.data;
      for ( var i in items ) {
        var item = items[ i ];
        var read_like_item = $( '#' + item.cd ).removeClass( 'invisible' );
        $( '>span:first-child', read_like_item ).text( item.read_num * 1 );
        $( '>span:last-child', read_like_item ).text( item.like_num * 1 );
      }
    } );
    ShowTitleMessage.hide();
    $( '[data-toggle="tooltip"]' ).tooltip( {
      trigger: 'hover'
    } );

  }

  function paging( page ) {
    if ( page > 10 ) {
      $( '.paging' ).show();
    } else {
      $( '.paging' ).hide();
      return;
    }
    $( ".paging" ).pagination( page, {
      num_edge_entries: 2,
      num_display_entries: 4,
      items_per_page: 10,
      prev_text: '上一页',
      next_text: '下一页',
      callback: function( page_index, jq ) {
        if ( public_vars.page ) {
          public_vars.page = false;
          return;
        } else {
          params_biz.data[ 'page' ] = page_index + 1;
          myAjax( params_biz );
        }
      }
    } );
  }
  paging( $( '.total_number' ).text() );

  get_msg_ext( $( '.article-sub .js_read_like' ).map( function() {
    return $( this ).prop( 'id' );
  } ).get().join( ',' ) );

  $( '[data-toggle="tooltip"]' ).tooltip();

  /*文章收藏点击*/
  public_vars.$wrap.on( "click", ".article-collect", function() {
    var re = $( this );
    var articleID = $( this ).parent().attr( 'data-articleID' );
    if ( re.hasClass( "golden" ) ) {
      $.ajax( {
        type: 'post',
        url: '/favorites/del',
        data: {
          id: articleID,
          rt: 'collectArt',
        },
        success: function( data ) {
          var json = typeof data === 'string' ? $.parseJSON( data ) : data;
          if ( json.ret == 0 ) {
            ShowTitleMessage.showWarning( '取消收藏成功！', showTime );
            re.removeClass( "golden" );
          }
        }
      } )
    } else {
      $.ajax( {
        type: 'post',
        url: BIGWE_API.COLLECT_ART.GET_COLLECT_ART,
        data: {
          cd: articleID,
          rt: "collectArt",
        },
        success: function( data ) {
          var data = typeof data === 'string' ? $.parseJSON( data ) : data;
          if ( data.ret == 0 || data.ret == 70004 ) {
            ShowTitleMessage.show( '收藏成功!', 1000 );
            re.addClass( "golden" );
          } else {
            ShowTitleMessage.show( data.msg, 1000 );
          }
        }
      } )
    }
  } );
} );